Imports System.Net
Imports System.Text

Public Class Servidor


    Private Tempo_verificacao As Integer
    Private Clientes As List(Of Cliente)

    Dim listaClientes As New Hashtable
    Sub Mensagem(ByVal texto As String)
        texto.Trim()
        Console.WriteLine(" >> " + texto)
    End Sub
    Private Sub broadcast(ByVal Mensagem As String, ByVal nomeUsuario As String, ByVal flag As Boolean)
        Dim Item As DictionaryEntry
        For Each Item In listaClientes
            Dim broadcastSocket As System.Net.Sockets.TcpClient
            broadcastSocket = CType(Item.Value, System.Net.Sockets.TcpClient)
            Try
                Dim broadcastStream As System.Net.Sockets.NetworkStream = broadcastSocket.GetStream()
                Dim broadcastBytes As [Byte]()
                If flag = True Then
                    broadcastBytes = Encoding.ASCII.GetBytes(nomeUsuario + " diz : " + Mensagem)
                Else
                    broadcastBytes = Encoding.ASCII.GetBytes(Mensagem)
                End If
                broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length)
                broadcastStream.Flush()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        Next
    End Sub
    Public Class tratarCliente
        Dim clientSocket As System.Net.Sockets.TcpClient
        Dim clNo As String
        Dim listaClientes As Hashtable
        Public Sub iniciaCliente(ByVal inClientSocket As System.Net.Sockets.TcpClient, ByVal clineNo As String, ByVal cList As Hashtable)
            Me.clientSocket = inClientSocket
            Me.clNo = clineNo
            Me.listaClientes = cList
            Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf handleClient)
            ctThread.Start()
        End Sub

        Private Sub handleClient()
            Dim contadorRequisicao As Integer
            Dim bytesFrom(10024) As Byte
            Dim dadosDoCliente As String
            Dim rContador As String
            contadorRequisicao = 0
            While (True)
                Try
                    contadorRequisicao = contadorRequisicao + 1
                    Dim networkStream As System.Net.Sockets.NetworkStream = clientSocket.GetStream()
                    networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
                    dadosDoCliente = System.Text.Encoding.ASCII.GetString(bytesFrom)
                    dadosDoCliente = dadosDoCliente.Substring(0, dadosDoCliente.IndexOf("$"))
                    rContador = Convert.ToString(contadorRequisicao)
                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try
            End While
        End Sub


    End Class
End Class
